import { Query, Store, StoreConfig } from "@datorama/akita";
import { produce } from 'immer';
import { SignInOption } from "@/models/request/roomReq";
import { LIVE_TYPE, ROOM_ROLE, ROOM_STATUS } from "@/constants/room";
import { Assistant, LiveStream, Room, File } from "@/models/LiveRoom";

function createInitialState(): Room {
  return {
    ui: {
      muted: false, //默认不静音
      debug: false, //默认不开启调试
      beauty: 2, //默认不开启美颜
      isFront: true, //默认前置摄像头
      mode: 'HD', //默认高清
      operation: "", //操作类型
      progress: 0,// 回放播放进度
      videoEnded: false, //回放播放结束
    },
    roomRole: ROOM_ROLE.AUDIENCE, //房间角色
    roomName: "", //直播间名称
    roomId: "", //房间ID
    orgId: "",
    orgName: "",
    orgLogo: "",
    tips: "",

    status: ROOM_STATUS.NOT_STARTED, //未开播
    direction: false, //房间方向
    inviteStatus: 0, //邀请状态
    muteStatus: 0, //禁言状态
    passwd: "",

    type: LIVE_TYPE.VIDEO, //默认是视频直播
    ownerId: "", //创建人ID
    ownerName: "", //创建人名字
    ownerLogo: "", //创建人头像
    anchorId: "",
    anchorName: "",
    anchorLogo: "",
    files: [], //文件列表
    assistants: [], //助手列表
    liveStreams: [], //主播流
    linkStreams: [], //连麦流
    picUrl: "", //房间图片
    preLoadVideo: "", //暖场视频
    pauseVideo: "", //过场视频
    description: "", //房间结束
    createTime: new Date(), //创建时间
    startTime: new Date(), //开始时间
    endTime: new Date(), //结束时间
    visits: 0, //访问人次
    shares: 0, //分享数
    likes: 0, //点赞数
    duration: 0, //直播或回放时长 
    forceSignIn: false, // 强制签到
    signInOptions: [], //签到选项
    allowShare: true, // 允许分享 
    audioStatus: false, // 直播的音频状态
    audienceToast: 0,  // 连麦的总人数 
  }
}

@StoreConfig({ name: "room", producerFn: produce, resettable: true })
export class RoomStore extends Store<Room> {
  private static _instance: RoomStore;

  constructor() {
    super(createInitialState())
  }

  //单例方法
  public static getInstance(): RoomStore {
    if (this._instance == null) {
      this._instance = new RoomStore();
    }
    return this._instance;
  }
}

export class RoomQuery extends Query<Room> {
  ui_muted$ = this.select(state => state.ui.muted);
  ui_debug$ = this.select(state => state.ui.debug);
  ui_beauty$ = this.select(state => state.ui.beauty);
  ui_isFront$ = this.select(state => state.ui.isFront);
  ui_mode$ = this.select(state => state.ui.mode);
  ui_operation$ = this.select(state => state.ui.operation);
  ui_progress$ = this.select(state => state.ui.progress);
  ui_videoEnded$ = this.select(state => state.ui.videoEnded);

  roomRole$ = this.select("roomRole");
  roomName$ = this.select("roomName");
  roomId$ = this.select("roomId");
  orgId$ = this.select("orgId");
  orgName$ = this.select("orgName");
  orgLogo$ = this.select("orgLogo");
  tips$ = this.select("tips");

  status$ = this.select("status");
  direction$ = this.select("direction");
  inviteStatus$ = this.select("inviteStatus");
  muteStatus$ = this.select("muteStatus");
  passwd$ = this.select("passwd");
  type$ = this.select("type");

  ownerId$ = this.select("ownerId");
  ownerName$ = this.select("ownerName");
  ownerLogo$ = this.select("ownerLogo");
  liveStreams$ = this.select("liveStreams");
  linkStreams$ = this.select("linkStreams");
  files$ = this.select("files");

  anchorId$ = this.select("anchorId");
  anchorName$ = this.select("anchorName");
  anchorLogo$ = this.select("anchorLogo");

  audioStatus$ = this.select("audioStatus")
  audienceToast$ = this.select("audienceToast")
  assistants$ = this.select("assistants");
  picUrl$ = this.select("picUrl");
  preLoadVideo$ = this.select("preLoadVideo");
  pauseVideo$ = this.select("pauseVideo");
  description$ = this.select("description");
  createTime$ = this.select("createTime");
  startTime$ = this.select("startTime");
  endTime$ = this.select("endTime");
  visits$ = this.select("visits");
  shares$ = this.select("shares");
  likes$ = this.select("likes");
  duration$ = this.select("duration");
  forceSignIn$ = this.select('forceSignIn')
  allowShare$ = this.select('allowShare');
  signInOptions$ = this.select('signInOptions');

  get ui_muted(): boolean {
    return this.getValue().ui.muted;
  }

  get ui_debug(): boolean {
    return this.getValue().ui.debug;
  }

  get ui_isFront(): boolean {
    return this.getValue().ui.isFront;
  }

  get ui_beauty(): number {
    return this.getValue().ui.beauty;
  }

  get ui_mode(): string {
    return this.getValue().ui.mode;
  }

  get ui_operation(): string {
    return this.getValue().ui.operation;
  }

  //原生值，非observable
  get roomId(): string {
    return this.getValue().roomId;
  }

  get orgId(): string {
    return this.getValue().orgId;
  }

  get roomName(): string {
    return this.getValue().roomName;
  }

  get ownerId(): string {
    return this.getValue().ownerId;
  }

  get ownerName(): string {
    return this.getValue().ownerName;
  }

  get ownerLogo(): string {
    return this.getValue().ownerLogo;
  }

  get anchorId(): string {
    return this.getValue().anchorId;
  }

  get anchorLogo(): string {
    return this.getValue().anchorLogo;
  }

  get audioStatus() : boolean {
    return this.getValue().audioStatus;
  }
  get audienceToast() : number {
    return this.getValue().audienceToast;
  }

  get assistants(): Assistant[] {
    return this.getValue().assistants;
  }

  get liveStreams(): LiveStream[] {
    return this.getValue().liveStreams;
  }

  get linkStreams(): LiveStream[] {
    return this.getValue().linkStreams;
  }

  get roomRole(): string {
    return this.getValue().roomRole;
  }

  get status(): number {
    return this.getValue().status;
  }

  get type(): number {
    return this.getValue().type;
  }

  get files(): File[] {
    return this.getValue().files;
  }

  get tips(): string {
    return this.getValue().tips;
  }

  get description(): string {
    return this.getValue().description;
  }

  get picUrl(): string {
    return this.getValue().picUrl;
  }

  get preLoadVideo(): string {
    return this.getValue().preLoadVideo;
  }

  get pauseVideo(): string {
    return this.getValue().pauseVideo;
  }

  get direction(): boolean {
    return this.getValue().direction;
  }

  get passwd(): string {
    return this.getValue().passwd
  }

  get forceSignIn(): boolean {
    return this.getValue().forceSignIn;
  }

  get allowShare(): boolean {
    return this.getValue().allowShare;
  }

  get startTime(): Date {
    return this.getValue().startTime;
  }

  get endTime(): Date {
    return this.getValue().endTime;
  }

  get ui_progress(): number {
    return this.getValue().ui.progress;
  }

  get ui_videoEnded(): boolean {
    return this.getValue().ui.videoEnded;
  }

  get signInOptions(): SignInOption[] {
    return this.getValue().signInOptions;
  }

  constructor(protected store: RoomStore) {
    super(store);
  }

}

export const makeStore = (): [RoomStore, RoomQuery] => {
  const store = RoomStore.getInstance();
  const query = new RoomQuery(store);
  return [store, query];
};
